<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_product_operation.php');
class ProductOperationControl
{
	var $product_operation_m;
	function __construct()
	{
		
		$this->product_operation_m = new ProductOperationModule();
	}
	/**
	* 增加或修改商品
	*
	* @param   string     $type                       类型 （add为增加商品， update为修改商品）
	* @param   int        $product_cbsid              cbs产品ID
	* @param   int        $product_sn                 产品编号
	* @param   string     $product_name               产品名称
	* @param   int        $product_version            版本号
	* @param   int        $product_brand              品牌
	* @param   int        $product_os                 存储平台表ID
	* @param   string     $product_isfree             是否免费
	* @param   string     $product_is_year_charge     是否按年收费（0否，1是）
	* @param   string     $product_type               是否试用
	* @param   string     $product_rel   			          对应产品
	* @param   string     $product_icon_min           Icon图标-小
	* @param   string     $product_icon_max           Icon图标-大
	* @param   string     $product_box_min            Box盒子图-小
	* @param   string     $product_box_middle   	    Box盒子图-中
	* @param   string     $product_box_big            Box盒子图-大
	* @param   int        $product_video_promo        Promo Demo URL
	* @param   int        $product_ad                 产品标语(广告语)
	* @param   string     $product_intro              产品简介（短描述）
	* @param   int        $product_content            产品简介（长描述
	* @param   int        $product_key_feature        产品Key Feature点
	* @param   int        $product_support_intro      系统支持说明-简单
	* @param   string     $product_support_content    系统支持说明-完整
	* @param   string     $techspec_content_table     技术参数表单
	* @param   string     $product_guide_content      guide详细内容
	* @param   string     $video_url                  视频url地址
	* @param   int        $video_width                视频宽
	* @param   int        $video_higth                 视频高
	* @param   int        $contrast_table             对比表内容
	* @param   int        $product_variant            产品变体
	* @return  int
	*/
	
	function  change_product($type,$product_id,$product_cbsid ,$product_sn,$product_name,$product_version ,$product_brand,$product_os,
	$product_isfree ,$product_is_year_charge,$product_type,$product_rel,$product_icon_min,$product_icon_max,$product_box_min,
	$product_box_middle,$product_box_big,$product_video_promo,$product_ad,$product_intro,$product_content,$product_key_feature,
	$product_support_intro,$product_support_content,$techspec_content_table,$product_guide_content,$video_url,$video_width,$video_higth
	,$contrast_table,$product_variant)
	{
		if($type == 'add')
		{
			$sql_num = "SELECT id    FROM won_product   WHERE won_product_id   = '$public_product_id' AND  won_product_delete = 0";
			$query = $GLOBALS['db']->query($sql_num);
			$num =  $GLOBALS['db']->num_rows($query);
			if($num == 0)
			{
				$sql ="INSERT INTO product  (product_cbsid ,product_sn,product_name,product_version ,product_brand,product_os,
				product_isfree ,product_is_year_charge,product_type,product_rel,product_icon_min,product_icon_max,product_box_min,
				product_box_middle,product_box_big,product_ad,product_intro,product_content,product_key_feature,
				product_support_intro,product_support_content,techspec_content_table,product_guide_content,product_add_user ,
				product_add_time,product_video_promo,video_width,video_higth ,product_contrast_table,product_variant) VALUES ('$product_cbsid','$product_sn','$product_name','$product_version','$product_brand',
				'$product_os', '$product_isfree','$product_is_year_charge','$product_type','$product_rel','$product_icon_min',
				'$product_icon_max','$product_box_min','$product_box_middle','$product_box_big','$product_ad',
				'$product_intro','$product_content','$product_key_feature','$product_support_intro','$product_support_content',
				'$techspec_content_table','$product_guide_content','".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."',
				'".$video_url."',$video_width,$video_higth,'$contrast_table','$product_variant') ";
			}else
			{
				return -1;
			}
		
		}elseif($type == 'update')
		{
			$sql = "UPDATE  product  SET product_os = '$product_os',product_isfree = '$product_isfree' ,product_is_year_charge = 
			'$product_is_year_charge',product_type = '$product_type',product_rel = '$product_rel',product_icon_min = '$product_icon_min'
			,product_icon_max = '$product_icon_max',product_box_min = '$product_box_min',product_box_middle = '$product_box_middle',
			product_box_big = '$product_box_big',product_video_promo = '$product_video_promo',product_ad = '$product_ad',product_intro 
			='$product_intro',product_content ='$product_content',product_key_feature = '$product_key_feature',product_support_intro = 
			'$product_support_intro',product_support_content = '$product_support_content',techspec_content_table = '$techspec_content_table',
			product_guide_content = '$product_guide_content' , product_update_time = '".$_SERVER['REQUEST_TIME']."',product_update_user = '".
			$_SESSION['AName']."',modify_status = 1 ,product_video_promo = '".$video_url."',video_width = '$video_width',video_higth = '$video_higth'
			,product_contrast_table = '$contrast_table',product_variant = '$product_variant'   WHERE   product_id  = '$product_id'  ";
			
			$affect = $this->product_operation_m->change_info($sql);
			return !empty($affect) ? $product_id : 0;
			
		}
		return $this->product_operation_m->get_insert_id($sql);		
	}

	/**
	* 增加feature 
	*
	* @param   string     $insert_str              cbs产品ID
	* @param   int        $product_id              产品id
	* @param   string     $type                    类型 （insert为增加feature ， m_insert为增加更多feature）
	* @return  int
	*/
	function  insert_feature_info($insert_str,$product_id,$type)
	{
		if($type == 'insert')
		{
			$sql ="INSERT INTO feature   (product_id  ,feature_title  ,feature_pic_min ,feature_pic_max 
			,feature_intro,feature_type , add_time , add_user,feature_order )  VALUES $insert_str ";
		}elseif($type == 'm_insert')
		{
			$sql ="INSERT INTO feature  (product_id  ,feature_title  ,more_feature_pic
			,feature_intro,feature_type ,add_time , add_user ,feature_order)  VALUES $insert_str ";
		}
		return $this->product_operation_m->change_info($sql);
	}

	/**
	* 得到产品信息
	* @param   int        $product_id              产品id
	* @return  array
	*/
	function get_product_info($product_id)
	{
		$sql = 'SELECT product_id,product_cbsid ,product_sn,product_name,product_version ,product_brand,product_os,
		product_isfree ,product_is_year_charge,product_type,product_rel,product_icon_min,product_icon_max,product_box_min,
		product_box_middle,product_box_big,product_video_promo,product_ad,product_intro,product_content,product_key_feature,
		product_support_intro,product_support_content,techspec_content_table,product_guide_content,video_width,
		video_higth,product_contrast_table,product_variant FROM product  WHERE  product_id  = '.$product_id;
		return $this->product_operation_m->get_product_info($sql);
	}
	/**
	* 得到feature
	* @param   int        $product_id              产品id
	* @param   string     $type                    类型
	* @return  array
	*/
	function get_feature_info($product_id,$type)
	{
		if($type == 'feature')
		{
			$sql = "SELECT product_id ,feature_id ,feature_title ,feature_intro ,feature_pic_min ,
			feature_pic_max,feature_order  FROM feature  WHERE product_id  = $product_id  AND feature_type  = 0 
			AND is_delete  =  0  ORDER BY feature_order ASC ";
		}elseif($type == 'm_feature')
		{
			$sql = "SELECT product_id ,feature_id ,feature_title ,feature_intro ,more_feature_pic ,feature_order 
			FROM feature  WHERE product_id  = $product_id  AND feature_type  = 1 AND is_delete  =  0  ORDER BY feature_order ASC ";
		}elseif($type == 'o_feature')
		{
			$sql = "SELECT product_id ,feature_id ,feature_title ,feature_intro ,more_feature_pic ,feature_order 
			FROM feature  WHERE product_id  = $product_id  AND feature_type  = 2 AND is_delete  =  0  ORDER BY feature_order ASC ";
		}
		return $this->product_operation_m->get_product_info($sql);
	}
	/**
	* 修改feature
	* @param   string        $type               类型 
	* @param   int        $feature_id           特点id
	* @param   int        $feature_intro        特点描述
	* @param   string     $feature_title        特点标题
	* @param   string     $more_feature_pic     更多配图
	* @param   string     $feature_pic_min      特点小图
	* @param   string     $feature_pic_max      特点大图
	* @param   string     $feature_order        排序
	* @return  int
	*/
	function  update_feature_info($type,$feature_id,$feature_intro = '',$feature_title = '',$more_feature_pic = '' ,
	$feature_pic_min = ''  ,$feature_pic_max = '' , $feature_order = '')
	{
		if($type == 'feature')
		{
			$sql = "UPDATE feature  SET feature_title = '".$feature_title."',feature_intro  = '".$feature_intro."' ,
			feature_title = '".$feature_title."',feature_pic_min  = '".$feature_pic_min ."',modify_status = 1,
			feature_pic_max  = '".$feature_pic_max ."' , update_time = '".$_SERVER['REQUEST_TIME']."' ,
			update_user  =  '".$_SESSION['AName']."' , feature_order = '".$feature_order."'   WHERE feature_id  = '".$feature_id."' ";
		}elseif($type == 'more_feature' || $type == 'other_feature'){
			$sql = "UPDATE feature  SET feature_title = '".$feature_title."',feature_intro  = '".$feature_intro ."' ,
			feature_title = '".$feature_title."',more_feature_pic   = '".$more_feature_pic ."', update_time = 
			'".$_SERVER['REQUEST_TIME']."' ,modify_status = 1,update_user  =  '".$_SESSION['AName']."' ,
			feature_order = '".$feature_order."'   WHERE  feature_id  = '".$feature_id."' ";
		}elseif($type == 'delete'){
			$sql =  'UPDATE feature SET is_delete  = 1  WHERE feature_id = '.$feature_id;	
		}
		
		return $this->product_operation_m->change_info($sql);
	}

	//note 删除ws的download页面在CMS的缓存
	function clear_ws_download_cache(){
		$file = glob('../image2/cache/ws_download-*.php');
		
		if (!empty($file)) {
			foreach( $file as $v ){
				@unlink( $v );
			}
		}
	}
	
	/**
	 * 取得产品所属品牌
	 * */
	function get_brand( $pid ){
		if( !$pid ) return;
		$sql = "SELECT product_brand FROM product WHERE product_id = {$pid}";
		$data = $this->product_operation_m->get_product_info( $sql );
		return $data[0]['product_brand'];
	}

}
?>